package com.sevenGroup.pawnshop.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sevenGroup.pawnshop.Utils.CopyProperties;
import com.sevenGroup.pawnshop.Utils.ParameterCalibrationUtil;
import com.sevenGroup.pawnshop.Utils.Result;
import com.sevenGroup.pawnshop.controller.form.SelectWarehousesForm;
import com.sevenGroup.pawnshop.controller.form.pageForm;
import com.sevenGroup.pawnshop.controller.form.WarehousesForm;
import com.sevenGroup.pawnshop.controller.info.WarehousesInfo;
import com.sevenGroup.pawnshop.exception.sevenGroupException;
import com.sevenGroup.pawnshop.model.Warehouses;
import com.sevenGroup.pawnshop.service.WarehouseStoreService;
import com.sevenGroup.pawnshop.service.WarehousesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author sevenGroup
 * @since 2022-02-16
 */
@RestController
@RequestMapping("/warehouses")
@Api(tags = "处理与仓库相关的接口")
public class WarehousesController {

    @Autowired
    private WarehousesService warehousesService;


    //新增仓库
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("新增仓库")
    @PostMapping("saveWarehouse")
    public Result saveWarehouse(@ApiParam("需要传入仓库对象")@Valid @RequestBody WarehousesForm warehousesForm, BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        warehousesService.saveWarehouse(warehousesForm);
        return Result.success(null);
    }

    //分页查询所有仓库
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("分页查询所有仓库")
    @PostMapping("selectAllWarehouses")
    public Result selectAllWarehouses(@ApiParam("需要传入分页对象")@Valid @RequestBody pageForm pageForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        IPage<WarehousesInfo> allWarehouses = warehousesService.selectAllWarehouses(pageForm);
        return Result.success(allWarehouses);
    }

    //查询所有仓库
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("查询所有仓库")
    @PostMapping("selectAllWarehouses1")
    public Result selectAllWarehouses1(){
        List<Warehouses> list = warehousesService.list();
        return Result.success(list);
    }

    //查询所有仓库
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("查询所有仓库")
    @PostMapping("selectAllWarehousesByWarehouseState")
    public Result selectAllWarehousesByWarehouseState(){
        List<Warehouses> list = warehousesService.list(new QueryWrapper<Warehouses>().eq("warehouse_state", "正常"));
        return Result.success(list);
    }


    //根据id删除仓库
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("根据id删除仓库")
    @PostMapping("delWarehouses")
    public Result delWarehouses(@ApiParam("需要传入仓库id")Integer warehousesId) throws sevenGroupException {
        warehousesService.delWarehouses(warehousesId);
        return Result.success(null);
    }

    //多选，根据集合删除仓库
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("多选，根据集合删除仓库")
    @PostMapping("delWarehousesByList")
    public Result delWarehousesByList(@ApiParam("需要传入仓库id集合")@RequestBody ArrayList<Integer> list) throws sevenGroupException {
        warehousesService.delWarehousesByList(list);
        return Result.success(null);
    }

    //更新仓库信息
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("更新仓库信息")
    @PostMapping("updateWarehouses")
    public Result updateWarehouses(@ApiParam("需要传入仓库对象")@Valid @RequestBody WarehousesForm warehousesForm,BindingResult bindingResult) throws sevenGroupException {
        ParameterCalibrationUtil.ParameterCalibration(bindingResult);
        warehousesService.updateWarehouses(warehousesForm);
        return Result.success(null);
    }

    //根据id查询仓库
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("根据id查询仓库")
    @PostMapping("selectWarehouseById")
    public Result selectWarehouseById(@ApiParam("仓库id")Integer warehouseId){
        Warehouses warehouses = warehousesService.getById(warehouseId);
        return Result.success(warehouses);
    }

    //根据条件查询所有仓库
    @PreAuthorize("hasAnyAuthority('boss')")
    @ApiOperation("根据条件查询所有仓库")
    @PostMapping("selectAllWarehousesByCondition")
    public Result selectAllWarehousesByCondition(@ApiParam("需要传入查询条件的门店对象")@RequestBody SelectWarehousesForm selectWarehousesForm) throws sevenGroupException {
        IPage iPage = warehousesService.selectAllWarehouses(selectWarehousesForm);
        return Result.success(iPage);
    }

    //根据门店id查仓库
    @PreAuthorize("hasAnyAuthority('boss','shopManage')")
    @ApiOperation("根据门店id查仓库")
    @GetMapping("selectWarehouseByStoreId/{storeId}")
    public Result selectWarehouseByStoreId(@ApiParam("需要门店Id")@PathVariable Integer storeId) throws sevenGroupException {
        List<Warehouses> warehouses = warehousesService.selectAllWarehousesByStoreId(storeId);
        return Result.success(warehouses);
    }

}

